/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sparql;

import java.io.FileNotFoundException;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;

/**
 *
 * @author riccardo
 */
public class SparqlDBPedia {

    /**
     * @param args the command line arguments
     */
  
    public static void main(String[] args) throws FileNotFoundException {
        String m= "http://dbpedia.org/sparql";
        String test="man;woman";
        while(test !=null && !test.equals("exit")){
        //    test=JOptionPane.showInputDialog("DBpedia");
            if(test !=null && test.contains(";")){
                String[] s=test.split(";");
                printResultQuery(predicatesConnectingTwoTerms(s[0], s[1]),m);
                printResultQuery(oneStepSimilarity(s[0], s[1]),m);
                //printResultQuery(twoStepsSimilarity(s[0], s[1]),m);
            }
            
        }
    }
    
    public static Query predicatesConnectingTwoTerms(String s1, String s2){
        return QueryFactory.create("PREFIX wordnet: <http://www.w3.org/2006/03/wn/wn20/schema/> "
                +"PREFIX id:   <http://wordnet.rkbexplorer.com/id/> "
                +"PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
                +"PREFIX dcterms: <http://purl.org/dc/terms/> "
                +"PREFIX skos: <http://www.w3.org/2004/02/skos/core#> "
                +"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
                +"PREFIX owl:  <http://www.w3.org/2002/07/owl#> "
                +"SELECT DISTINCT ?p WHERE{"
                + "      {?x rdfs:label '"+s1+"'@en . ?y rdfs:label '"+s2+"'@en . ?y ?p ?x} "
                + "UNION {?x rdfs:label '"+s1+"'@en . ?y rdfs:label '"+s2+"'@en . ?x ?p ?y}} LIMIT 1000");
    }
    
    public static Query oneStepSimilarity(String s1, String s2){
        return QueryFactory.create("PREFIX wordnet: <http://www.w3.org/2006/03/wn/wn20/schema/> "
                +"PREFIX id:   <http://wordnet.rkbexplorer.com/id/> "
                +"PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
                +"PREFIX dcterms: <http://purl.org/dc/terms/> "
                +"PREFIX skos: <http://www.w3.org/2004/02/skos/core#> "
                +"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
                +"PREFIX owl:  <http://www.w3.org/2002/07/owl#> "
                +"SELECT DISTINCT ?p1 ?z ?p2 WHERE{ "
                + "     {{ ?y rdfs:label '"+s1+"'@en . ?x rdfs:label '"+s2+"'@en .?y ?p2 ?z . ?x ?p1 ?z . FILTER(?p1 != rdf:type && ?p2 != rdf:type)}"
                + "UNION { ?y rdfs:label '"+s1+"'@en . ?x rdfs:label '"+s2+"'@en .?z ?p2 ?y . ?z ?p1 ?x . FILTER(?p1 != rdf:type && ?p2 != rdf:type)}"
                + "UNION { ?y rdfs:label '"+s1+"'@en . ?x rdfs:label '"+s2+"'@en .?z ?p2 ?y . ?x ?p1 ?z . FILTER(?p1 != rdf:type && ?p2 != rdf:type)}"
                + "UNION { ?y rdfs:label '"+s1+"'@en . ?x rdfs:label '"+s2+"'@en .?y ?p2 ?z . ?z ?p1 ?x . FILTER(?p1 != rdf:type && ?p2 != rdf:type)}}} LIMIT 1000");
    }
    
    public static Query twoStepsSimilarity(String s1, String s2){
        return QueryFactory.create("PREFIX wordnet: <http://www.w3.org/2006/03/wn/wn20/schema/> "
                +"PREFIX id:   <http://wordnet.rkbexplorer.com/id/> "
                +"PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
                +"PREFIX dcterms: <http://purl.org/dc/terms/> "
                +"PREFIX skos: <http://www.w3.org/2004/02/skos/core#> "
                +"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
                +"PREFIX owl:  <http://www.w3.org/2002/07/owl#> "
                +"SELECT DISTINCT ?p1 ?z1 ?p3 ?z2 ?p2 WHERE{ "
                + "     {{ ?y rdfs:label '"+s1+"'@en . ?x rdfs:label '"+s2+"'@en .?y ?p2 ?z2 . ?x ?p1 ?z1 . ?z1 ?p3 ?z2 . FILTER(?z2 != ?x && ?z1 != ?y && ?p1 != rdf:type && ?p2 != rdf:type && ?p3 != rdfs:label)}"
                + "UNION { ?y rdfs:label '"+s1+"'@en . ?x rdfs:label '"+s2+"'@en .?z2 ?p2 ?y . ?z1 ?p1 ?x . ?z1 ?p3 ?z2 . FILTER(?z2 != ?x && ?z1 != ?y && ?p1 != rdf:type && ?p2 != rdf:type && ?p3 != rdfs:label)}"
                + "UNION { ?y rdfs:label '"+s1+"'@en . ?x rdfs:label '"+s2+"'@en .?z2 ?p2 ?y . ?x ?p1 ?z1 . ?z1 ?p3 ?z2 . FILTER(?z2 != ?x && ?z1 != ?y && ?p1 != rdf:type && ?p2 != rdf:type && ?p3 != rdfs:label)}"
                + "UNION { ?y rdfs:label '"+s1+"'@en . ?x rdfs:label '"+s2+"'@en .?y ?p2 ?z2 . ?z1 ?p1 ?x . ?z1 ?p3 ?z2 . FILTER(?z2 != ?x && ?z1 != ?y && ?p1 != rdf:type && ?p2 != rdf:type && ?p3 != rdfs:label)}"
                + "UNION { ?y rdfs:label '"+s1+"'@en . ?x rdfs:label '"+s2+"'@en .?y ?p2 ?z2 . ?x ?p1 ?z1 . ?z2 ?p3 ?z1 . FILTER(?z2 != ?x && ?z1 != ?y && ?p1 != rdf:type && ?p2 != rdf:type && ?p3 != rdfs:label)}"
                + "UNION { ?y rdfs:label '"+s1+"'@en . ?x rdfs:label '"+s2+"'@en .?z2 ?p2 ?y . ?z1 ?p1 ?x . ?z2 ?p3 ?z1 . FILTER(?z2 != ?x && ?z1 != ?y && ?p1 != rdf:type && ?p2 != rdf:type && ?p3 != rdfs:label)}"
                + "UNION { ?y rdfs:label '"+s1+"'@en . ?x rdfs:label '"+s2+"'@en .?z2 ?p2 ?y . ?x ?p1 ?z1 . ?z2 ?p3 ?z1 . FILTER(?z2 != ?x && ?z1 != ?y && ?p1 != rdf:type && ?p2 != rdf:type && ?p3 != rdfs:label)}"
                + "UNION { ?y rdfs:label '"+s1+"'@en . ?x rdfs:label '"+s2+"'@en .?y ?p2 ?z2 . ?z1 ?p1 ?x . ?z2 ?p3 ?z1 . FILTER(?z2 != ?x && ?z1 != ?y && ?p1 != rdf:type && ?p2 != rdf:type && ?p3 != rdfs:label)}}} LIMIT 1000");
    }
    
    public static void printResultQuery(Query query, String service){
        
        QueryExecution e=QueryExecutionFactory.sparqlService(service,query);
        
        ResultSet execSelect = e.execSelect();
        int i=0;
            while(execSelect.hasNext()){
                System.out.println(execSelect.next());
                i++;
            }
            System.out.println(i);
        e.close();
    }
    
    public static ResultSet executeQuery(Query query, String service){
        QueryExecution e=QueryExecutionFactory.sparqlService(service,query);
        ResultSet ret=e.execSelect();
        e.close();
        return ret;
    }
}